home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / itoa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.3 KB  |  79 lines

  1. #include "lib.h"
  2.  
  3. /* Integer to ASCII for signed decimal integers. */
  4.  
  5. static int next;
  6. #ifdef __GNUC__  /* ACK cc does not handle `#if defined..' correctly so */
  7.          /* we have to do these conditionals this way */
  8. #ifndef __MSHORT__
  9. static char qbuf[14];
  10. #else
  11. static char qbuf[8];
  12. #endif
  13. #else
  14. static char qbuf[8];
  15. #endif
  16.  
  17. char *itoa(n)
  18. int n;
  19. {
  20.   register int r, k;
  21.   int flag = 0;
  22.  
  23.   /* bug fix - did'nt handle -32768 correctly
  24.    *     ++jrb bammi@dsrgsun.ces.cwru.edu
  25.    */
  26. #ifdef __GNUC__
  27. #ifdef __MSHORT__
  28.   if(n == -32768) {    /* ACK C Users WATCH OUT, ITS BRAINDAMAGED, and the
  29.                explaination forthcoming for this on the net
  30.                from the people at V..U was even worse ,
  31.                Gnu C does this correctly */
  32.       strcpy(qbuf, "-32768");
  33.       return qbuf;
  34.   }
  35. #else
  36.   if(n == -2147483648L) {    /* ACK C barfs here */
  37.       strcpy(qbuf, "-2147483648");
  38.       return qbuf;
  39.   }
  40. #endif
  41. #else
  42.   if(n == -32768) {
  43.       strcpy(qbuf, "-32768");
  44.       return qbuf;
  45.   }
  46. #endif
  47.       
  48.   next = 0;
  49.   if (n < 0) {
  50.     qbuf[next++] = '-';
  51.     n = -n;
  52.   }
  53.   if (n == 0) {
  54.     qbuf[next++] = '0';
  55.   } else {
  56.  
  57. #ifdef __GNUC__
  58. #ifdef __MSHORT__
  59.     k = 10000;
  60. #else
  61.     k = 1000000000;
  62. #endif
  63. #else
  64.     k = 10000;
  65. #endif
  66.     while (k > 0) {
  67.         r = n/k;
  68.         if (flag || r > 0) {
  69.             qbuf[next++] = '0' + r;
  70.             flag = 1;
  71.         }
  72.         n -= r * k;
  73.         k = k/10;
  74.     }
  75.   }
  76.   qbuf[next] = 0;
  77.   return(qbuf);
  78. }
  79.